package com.zut.eduservice.controller;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zut.commonutils.R;
import com.zut.eduservice.entity.InformationSecurity;
import com.zut.eduservice.entity.vo.InformationQuery;
import com.zut.eduservice.service.InformationSecurityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-08
 */
@Api(description="信息管理")
@RestController
@RequestMapping("/eduservice/informationSecurity")
@CrossOrigin
public class InformationSecurityController {

     @Autowired
    private InformationSecurityService informationSecurityService;

     //查询所有数据
    @GetMapping("findAll")
    public List<InformationSecurity> findinformation(){
        List<InformationSecurity> list = informationSecurityService.list(null);
        return list;
    }


    //分页查询所有数据
    @GetMapping("pageInformation/{current}/{limit}")
    public R pageListInformation(@PathVariable long current,
                             @PathVariable long limit){
        Page<InformationSecurity> pageInformation =new Page<>(current,limit);
         informationSecurityService.page(pageInformation, null);
        long total = pageInformation.getTotal();
       List<InformationSecurity> records= pageInformation.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //条件分页查询
    @PostMapping("pageInformationCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) InformationQuery informationQuery) {
        //创建page对象
        Page<InformationSecurity> pageInformation = new Page<>(current,limit);

        //构建条件
        QueryWrapper<InformationSecurity> wrapper =new QueryWrapper<>();
        String name = informationQuery.getName();
        String severity = informationQuery.getSeverity();
        String begin = informationQuery.getBegin();
        String end = informationQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);

        }
       if(!StringUtils.isEmpty(severity)){
           wrapper.eq("severity",severity);
       }

        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("published",begin);
        }

        if(!StringUtils.isEmpty(end)){
            wrapper.le("published",end);
        }
        //根据时间进行排序
        wrapper.orderByDesc("published");
        //调用方法实现条件查询分页
        informationSecurityService.page(pageInformation,wrapper);
        long total = pageInformation.getTotal();
        List<InformationSecurity> records= pageInformation.getRecords();
        return R.ok().data("total",total).data("records",records);
    }


    //添加方法接口
    @PostMapping("addInformation")
    public R addTeacher(@RequestBody InformationSecurity eduInformation) {
        boolean save = informationSecurityService.save(eduInformation);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    //根据id进行查询，为修改接口进行铺垫
    @GetMapping("getInformmation/{id}")
    public R getInformation(@PathVariable int id){
       InformationSecurity informationSecurity= informationSecurityService.getById(id);
        return R.ok().data("information",informationSecurity);
    }
    //修改方法接口
    @PostMapping("updataInformmation")
    public R updataInformation(@RequestBody InformationSecurity informationSecurity){
        boolean flag  = informationSecurityService.updateById(informationSecurity);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //逻辑删除方法
    @ApiOperation(value = "逻辑删除方法")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id",  required = true)
                           @PathVariable int id) {
        boolean flag =  informationSecurityService.removeById(id);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

